package com.smy.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smy.reggie.common.R;
import com.smy.reggie.pojo.Employee;
import com.smy.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1.将页面传输的密码mb5加密
        String pwd = employee.getPassword();
        pwd = DigestUtils.md5DigestAsHex(pwd.getBytes());
        //2.将页面数据提交到数据库
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);
        //3.判断用户是否存在
        if (emp == null)return R.error("登录失败");
        //4.判断密码是否正确
        if (!emp.getPassword().equals(pwd))return R.error("登录失败");
        //5.判断该用户是否为可用状态
        if (emp.getStatus() == 0)return R.error("该账号已被禁用！");
        //6.将用户存入session放行
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 登出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工
     */
    @PostMapping
    public R<String> addEmployee(HttpServletRequest request,@RequestBody Employee employee){
        log.info("当前添加员工信息：{}",employee);
        //默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //创建和修改时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());

        //创建者和修改者
        //Long empId = (Long)request.getSession().getAttribute("employee");
        //employee.setCreateUser(empId);
        //employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增员工成功");
        }

    /**
     * 分页名称查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> selectByPage(Long page,Long pageSize,String name){
        log.info("page = " + page + " , pageSize = " + pageSize + " , name = " + name);
        Page<Employee> r = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getCreateTime);
        employeeService.page(r,queryWrapper);
        return R.success(r);
    }

    /**
     * 修改
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        log.info(employee.toString()+"为当前修改员工");

        //Long empId =(Long)request.getSession().getAttribute("employee");
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");

    }
    /**
     * 修改界面回显
     */
    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable Long id){
        return R.success(employeeService.getById(id));
    }
}
